
var config_obj = {
	"user_name":"我",
	"ai_name":"ai",
	"user_head_img":"head1.jpg",
	"ai_head_img":"head2.jpg",
	"chat_api_url":"",
	"sound_api_url":"",
};


$.get("config/config.json",function(data,status){
        //alert("数据: " + data + "\n状态: " + status);
		if(data){
			config_obj = data;
			init();
		}
});

//$("#chat-box").html($("#exmple").html());

var historyArr = [];
	
	
function initUserNameHeadImg(){
	$("#exmple .row-mine .head-img img").attr("src",config_obj.user_head_img);
	$("#exmple .row-mine .user-name").html(config_obj.user_name);
	$("#exmple .row-them .head-img img").attr("src",config_obj.ai_head_img);
	$("#exmple .row-them .user-name").html(config_obj.ai_name);
}
function initChatHistory(){
	historyArr = loadHistory();
	if(historyArr && historyArr.length>0){
		for(var i=0;i<historyArr.length;i++){
			say(historyArr[i][0],0);
			say(historyArr[i][1],1);
		}
		
	}
}

function init(){
	
	initUserNameHeadImg();
	initChatHistory();
	
}




	
	function saveHistory(arr){
		if (typeof(Storage) == "undefined") {
			return false;
		}
		//console.log(arr);
		localStorage.setItem("history",JSON.stringify(arr));//   
		//console.log(JSON.parse(localStorage.getItem("history")));
	}
	function loadHistory(){
		if (typeof(Storage) == "undefined") {
			return false;
		}
		var history = [];
		try {
			history = JSON.parse(localStorage.getItem("history"));// 
		} catch(err){
		}
		
		
		if(!history){
			history=[];
		}
		return history;
	}
	
	
	function submitInputOnChange(text){
		if(text!=""){
			$("#submit-button").addClass("button-light")
		}else{
			$("#submit-button").removeClass("button-light")
		}
	}
	
	$("#submit-input").keyup(function(){
		var text = $(this).val();
		submitInputOnChange(text);
	});
	
	$("#submit-input").change(function(){
		var text = $(this).val();
		submitInputOnChange(text);
	});
	
	
	
	$("#chat-form").submit(function(){
		var text = $("#submit-input").val();
		if(text==""){
			//layer.msg("请输入聊天内容")
			return false;
		}
		//console.log(text);
		var historyNewRow = [];
		
		var res = say(text,0);
		if(res){
			$("#submit-input").val("");
		}
		//滚动到底部
		$("html, body").animate({ scrollTop: $(document).height() }, 300);
		callChatApi(text,historyArr,function(replayMsg){
			var res = say(replayMsg,1);
			//滚动到底部
			$("html, body").animate({ scrollTop: $(document).height() }, 300);
			 layer.msg('语音生成中，请稍候',{"offset":['300px', '50px']});
			ajaxGetSoundPathByText(replayMsg,function(path){
				playMusic(path);
			})
			historyNewRow.push(text,replayMsg);
			historyArr.push(historyNewRow);
			saveHistory(historyArr);
		});
		return false;
	});
	
	function say(text,target){
		if(text==""){
			return false;
		}
		var chat_row;
		if(target==0){
			chat_row = $("#exmple .row-mine").clone();
		}else if(target==1){
			chat_row = $("#exmple .row-them").clone();
		}else{
			return false;
		}
		chat_row.find(".chat-content").html(text);
		//console.log(chat_row);
		$("#chat-box").append(chat_row);
		return true;
	}
	
	function callChatApi(text,historyArr,callBack){
		//这里调用api
		//console.log(config_obj.chat_api_url)
		var replayMsg = "暂无回复";
		
		chatPlugin.chat_api_funcion(config_obj.chat_api_url,text,historyArr,callBack);
		
	}
	
	$("#claer-button").click(function(){
		layer.confirm("是否清除聊天记录",{"title":"请确认","offset":['300px', '50px']},function(index){
			//console.log("是");
			$("#chat-box").html("");
			historyArr=[];
			saveHistory([])
			layer.close(index)
		},function(index){
			//console.log("否");
			layer.close(index)
		});
		
	});
	
	
	var audio = document.createElement("audio");
	function playMusic(music_src){
		audio.src = music_src;
		audio.play();
	}
	function pauseMusic(){
		audio.pause();
	}

	
	
	function ajaxGetSoundPathByText(text,callBack){
		
		chatPlugin.sound_api_funcion(config_obj.sound_api_url,text,callBack);
		
	}
	
	
	
	
	var lastClickEle = null;
	var date1=null;
	var intervalMax=300;
	$("#chat-box").on("click",".chat-row",function(){
	
		var sameEle = false;
		if(lastClickEle == this){
			//console.log("同一个元素");
			sameEle = true;
		}else{
			//console.log("不同元素");
		}
		lastClickEle = this;
		var interval = 999999999999;
		var date2=new Date();  //结束时间 
		if(date1){
			interval = date2.getTime()-date1.getTime();
			//console.log("时间间隔"+interval);
		}
		date1 = date2;
		if(sameEle && interval<intervalMax){
			 //console.log("是双击");
			 var text = $(this).find(".chat-content").html();
			 //console.log("内容为:"+text);
			 layer.msg('语音生成中，请稍候',{"offset":['300px', '50px']});
			ajaxGetSoundPathByText(text,function(path){
				playMusic(path);
			})
			 //这里使用text去api请求生成音频地址，然后播放
			 //playMusic("horse.mp3")
			 //pauseMusic()
		}
	});
	//$(".chat-row").click(function(){

	//})
	
	